#include "GBmp.hpp"
#include "camera_test.h"
Q_DECLARE_METATYPE(HMODULE);

camera_test::camera_test(QWidget *parent) : QMainWindow(parent)
{
    HMODULE hDll = LoadLibraryA("captureVirtual.dll");

    ui.setupUi(this);
    connect(ui.pushButtonOpen, SIGNAL(clicked()), this, SLOT(OnClickOpenCamera()));
    connect(ui.pushButtonClose, SIGNAL(clicked()), this, SLOT(OnClickCloseCamera()));

    FCreateFactory FCrateFac = (FCreateFactory)GetProcAddress(hDll, "CreateFactory");
    if (FCrateFac) {
        GCaptureFactory* fac = FCrateFac();
        fac->InitGlobal();
        m_pCamera = fac->ConnectCamera(11, ImageProcessCallback, CameraEventCallback, this);
    }
}

void __stdcall camera_test::ImageProcessCallback(int iCamId, const BYTE* pImage, void* pContext)
{
    camera_test* pThis = static_cast<camera_test*>(pContext);
    float fUsageRate = pThis->m_pCamera->GetUsageRate();
    GBmp bmp(pImage, pThis->m_pCamera->GetImageWidth(), pThis->m_pCamera->GetImageHeight(), pThis->m_pCamera->GetPixelBytes() == 1);
    bmp.SaveBmp("E:\\test\\test.bmp");
}

void __stdcall camera_test::CameraEventCallback(int iCamId, __int64 evt, void* pContext)
{

}

void camera_test::OnClickOpenCamera()
{
    m_pCamera->Start();
}

void camera_test::OnClickCloseCamera()
{
    m_pCamera->Stop();
}
